home *** CD-ROM | disk | FTP | other *** search
/ PC World Komputer 2010 April / PCWorld0410.iso / hity wydania / Ubuntu 9.10 PL / karmelkowy-koliberek-9.10-netbook-remix-PL.iso / casper / filesystem.squashfs / usr / share / hplip / base / module.py < prev    next >
Text File  |  2009-10-09  |  25KB  |  651 lines

  1. # -*- coding: utf-8 -*-
  2. #
  3. # (c) Copyright 2003-2009 Hewlett-Packard Development Company, L.P.
  4. #
  5. # This program is free software; you can redistribute it and/or modify
  6. # it under the terms of the GNU General Public License as published by
  7. # the Free Software Foundation; either version 2 of the License, or
  8. # (at your option) any later version.
  9. #
  10. # This program is distributed in the hope that it will be useful,
  11. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  13. # GNU General Public License for more details.
  14. #
  15. # You should have received a copy of the GNU General Public License
  16. # along with this program; if not, write to the Free Software
  17. # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
  18. #
  19. # Author: Don Welch
  20. #
  21.  
  22. # Std Lib
  23. import sys
  24. import re
  25. import getopt
  26. import os
  27.  
  28. # Local
  29. from g import *
  30. import tui, utils, device
  31.  
  32. USAGE_FLAG_NONE = 0x00
  33. USAGE_FLAG_DEVICE_ARGS = 0x01
  34. USAGE_FLAG_SUPRESS_G_DEBUG_FLAG = 0x02
  35. USAGE_FLAG_FILE_ARGS = 0x04
  36.  
  37.  
  38.  
  39. class Module(object):
  40.     def __init__(self, mod, title, version, doc,
  41.                  usage_data=None, avail_modes=None,
  42.                  supported_ui_toolkits=None,
  43.                  run_as_root_ok=False, quiet=False):
  44.  
  45.         self.mod = mod
  46.         self.title = title
  47.         self.version = version
  48.         self.doc = doc
  49.         self.usage_data = usage_data
  50.         os.umask(0037)
  51.         log.set_module(mod)
  52.         self.args = []
  53.         self.quiet = quiet
  54.         self.lock_file = None
  55.         prop.prog = sys.argv[0]
  56.  
  57.         if os.getenv("HPLIP_DEBUG"):
  58.             log.set_level('debug')
  59.  
  60.         self.avail_modes = avail_modes
  61.         if supported_ui_toolkits is not None:
  62.             self.supported_ui_toolkits = supported_ui_toolkits
  63.             self.num_supported_ui_toolkits = len(self.supported_ui_toolkits)
  64.         else:
  65.             self.supported_ui_toolkits = []
  66.             self.num_supported_ui_toolkits = 0
  67.  
  68.         self.default_ui_toolkit = sys_conf.get('configure', 'ui-toolkit', 'qt4')
  69.  
  70.         self.num_installed_ui_toolkits = 0
  71.         self.installed_ui_toolkits = []
  72.         if utils.to_bool(sys_conf.get('configure', 'qt3', '0')):
  73.             self.installed_ui_toolkits.append(UI_TOOLKIT_QT3)
  74.             self.num_installed_ui_toolkits += 1
  75.  
  76.         if utils.to_bool(sys_conf.get('configure', 'qt4', '0')):
  77.             self.installed_ui_toolkits.append(UI_TOOLKIT_QT4)
  78.             self.num_installed_ui_toolkits += 1
  79.  
  80.         self.default_mode = INTERACTIVE_MODE
  81.  
  82.         self.num_valid_modes = 0
  83.         if self.avail_modes is not None:
  84.             if GUI_MODE in self.avail_modes and prop.gui_build and self.installed_ui_toolkits:
  85.                 self.num_valid_modes += 1
  86.  
  87.             if INTERACTIVE_MODE in self.avail_modes:
  88.                 self.num_valid_modes += 1
  89.  
  90.             if NON_INTERACTIVE_MODE in self.avail_modes:
  91.                 self.num_valid_modes += 1
  92.  
  93.         if self.avail_modes is not None:
  94.             if INTERACTIVE_MODE in self.avail_modes:
  95.                 self.default_mode = INTERACTIVE_MODE
  96.  
  97.             elif NON_INTERACTIVE_MODE in self.avail_modes:
  98.                 self.default_mode = NON_INTERACTIVE_MODE
  99.  
  100.         if self.supported_ui_toolkits is not None and prop.gui_build and self.installed_ui_toolkits:
  101.  
  102.             if self.default_ui_toolkit == 'qt3' and UI_TOOLKIT_QT4 in self.supported_ui_toolkits and \
  103.                 UI_TOOLKIT_QT3 not in self.supported_ui_toolkits and INTERACTIVE_MODE in self.avail_modes:
  104.  
  105.                 # interactive + qt4 and default is qt3 --> set to interactive (if avail) (e.g., hp-align)
  106.                 self.default_mode = INTERACTIVE_MODE
  107.                 self.default_ui_toolkit = 'none'
  108.  
  109.             elif (UI_TOOLKIT_QT4 in self.supported_ui_toolkits and self.default_ui_toolkit == 'qt4' and UI_TOOLKIT_QT4 in self.installed_ui_toolkits) or \
  110.                  (UI_TOOLKIT_QT3 in self.supported_ui_toolkits and self.default_ui_toolkit == 'qt3' and UI_TOOLKIT_QT3 in self.installed_ui_toolkits):
  111.  
  112.                 self.default_mode = GUI_MODE
  113.  
  114.             elif self.default_ui_toolkit == 'qt3' and UI_TOOLKIT_QT3 not in self.supported_ui_toolkits:
  115.  
  116.                 if UI_TOOLKIT_QT4 in self.supported_ui_toolkits and UI_TOOLKIT_QT4 in self.installed_ui_toolkits: # (e.g, hp-linefeedcal?)
  117.                     self.default_ui_toolkit = 'qt4'
  118.                     self.default_mode = GUI_MODE
  119.  
  120.                 elif INTERACTIVE_MODE in self.avail_modes:
  121.                     self.default_mode = INTERACTIVE_MODE
  122.  
  123.                 elif NON_INTERACTIVE_MODE in self.avail_modes:
  124.                     self.default_mode = NON_INTERACTIVE_MODE
  125.  
  126.                 else:
  127.                     log.error("%s cannot be run using Qt3 toolkit." % self.mod)
  128.                     sys.exit(1)
  129.  
  130.             elif self.default_ui_toolkit == 'qt4' and UI_TOOLKIT_QT4 not in self.supported_ui_toolkits:
  131.  
  132.                 if UI_TOOLKIT_QT3 in self.supported_ui_toolkits and UI_TOOLKIT_QT3 in self.installed_ui_toolkits: # (e.g., hp-unload)
  133.                     self.default_ui_toolkit = 'qt3'
  134.                     self.default_mode = GUI_MODE
  135.  
  136.                 elif INTERACTIVE_MODE in self.avail_modes:
  137.                     self.default_mode = INTERACTIVE_MODE
  138.  
  139.                 elif NON_INTERACTIVE_MODE in self.avail_modes:
  140.                     self.default_mode = NON_INTERACTIVE_MODE
  141.  
  142.                 else:
  143.                     log.error("%s cannot be run using Qt4 toolkit." % self.mod)
  144.                     sys.exit(1)
  145.  
  146.  
  147.         self.mode = self.default_mode
  148.  
  149.         #log.debug("Default ui-toolkit: %s" % self.default_ui_toolkit)
  150.         #log.debug("Default mode: %s" % self.default_mode)
  151.  
  152.         if os.getuid() == 0 and not run_as_root_ok:
  153.             log.warn("%s should not be run as root/superuser." % mod)
  154.  
  155.  
  156.     def setUsage(self, include_flags=0, extra_options=None,
  157.                  extra_notes=None, see_also_list=None):
  158.  
  159.         if self.doc:
  160.             self.usage_data = [(self.doc, "", "name", True)]
  161.         else:
  162.             self.usage_data = []
  163.  
  164.         summary = ['Usage:', self.mod]
  165.         content = []
  166.         notes = []
  167.  
  168.         if include_flags & USAGE_FLAG_DEVICE_ARGS == USAGE_FLAG_DEVICE_ARGS:
  169.             summary.append('[DEVICE_URI|PRINTER_NAME]')
  170.             content.append(utils.USAGE_ARGS)
  171.             content.append(utils.USAGE_DEVICE)
  172.             content.append(utils.USAGE_PRINTER)
  173.  
  174.         if self.avail_modes is not None and self.num_valid_modes > 1:
  175.             summary.append('[MODE]')
  176.             content.append(utils.USAGE_SPACE)
  177.             content.append(utils.USAGE_MODE)
  178.  
  179.             if self.num_installed_ui_toolkits > 0:
  180.                 if GUI_MODE in self.avail_modes and prop.gui_build:
  181.                     content.append(utils.USAGE_GUI_MODE)
  182.  
  183.             if INTERACTIVE_MODE in self.avail_modes:
  184.                 content.append(utils.USAGE_INTERACTIVE_MODE)
  185.  
  186.             if NON_INTERACTIVE_MODE in self.avail_modes:
  187.                 content.append(utils.USAGE_NON_INTERACTIVE_MODE)
  188.  
  189.         # [options]
  190.         summary.append('[OPTIONS]')
  191.         content.append(utils.USAGE_SPACE)
  192.         content.append(utils.USAGE_OPTIONS)
  193.  
  194.         if self.avail_modes is not None and GUI_MODE in self.avail_modes and \
  195.             self.supported_ui_toolkits is not None and self.num_supported_ui_toolkits > 0 and \
  196.             prop.gui_build and self.num_installed_ui_toolkits > 0:
  197.  
  198.             if UI_TOOLKIT_QT3 in self.supported_ui_toolkits and UI_TOOLKIT_QT3 in self.installed_ui_toolkits:
  199.                 content.append(utils.USAGE_USE_QT3)
  200.  
  201.             if UI_TOOLKIT_QT4 in self.supported_ui_toolkits and UI_TOOLKIT_QT4 in self.installed_ui_toolkits:
  202.                 content.append(utils.USAGE_USE_QT4)
  203.  
  204.         content.append(utils.USAGE_LOGGING1)
  205.         content.append(utils.USAGE_LOGGING2)
  206.         if include_flags & USAGE_FLAG_SUPRESS_G_DEBUG_FLAG != USAGE_FLAG_SUPRESS_G_DEBUG_FLAG:
  207.             content.append(utils.USAGE_LOGGING3) # Issue with --gg in hp-sendfax
  208.  
  209.         # -q/--lang
  210.         #if self.avail_modes is not None and GUI_MODE in self.avail_modes and prop.gui_build:
  211.         #    content.append(utils.USAGE_LANGUAGE)
  212.  
  213.         content.append(utils.USAGE_HELP)
  214.  
  215.         if extra_options is not None:
  216.             for e in extra_options:
  217.                 content.append(e)
  218.  
  219.         # [FILES]
  220.         if include_flags & USAGE_FLAG_FILE_ARGS:
  221.             summary.append('[FILES]')
  222.  
  223.         # Notes
  224.         if extra_notes is not None or notes:
  225.             content.append(utils.USAGE_SPACE)
  226.             content.append(utils.USAGE_NOTES)
  227.  
  228.             for n in notes:
  229.                 content.append(n)
  230.  
  231.             if extra_notes is not None:
  232.                 for n in extra_notes:
  233.                     content.append(n)
  234.  
  235.         # See Also
  236.         if see_also_list is not None:
  237.             content.append(utils.USAGE_SPACE)
  238.             content.append(utils.USAGE_SEEALSO)
  239.             for s in see_also_list:
  240.                 content.append((s, '', 'seealso', False))
  241.  
  242.         content.insert(0, (' '.join(summary), '', 'summary', True))
  243.  
  244.         for c in content:
  245.             self.usage_data.append(c)
  246.  
  247.  
  248.     def parseStdOpts(self, extra_params=None,
  249.                      extra_long_params=None,
  250.                      handle_device_printer=True,
  251.                      supress_g_debug_flag=False):
  252.  
  253.         params = 'l:h' # 'l:hq:'
  254.         if not supress_g_debug_flag:
  255.             params = ''.join([params, 'g'])
  256.  
  257.         long_params = ['logging=', 'help', 'help-rest', 'help-man',
  258.                        'help-desc',
  259.                        #'lang=', 'loc=',
  260.                        'debug', 'dbg']
  261.  
  262.         if handle_device_printer:
  263.             params = ''.join([params, 'd:p:P:'])
  264.             long_params.extend(['device=', 'device-uri=', 'printer=', 'printer-name'])
  265.  
  266.         if self.num_valid_modes > 1:
  267.             if GUI_MODE in self.avail_modes and prop.gui_build:
  268.                 params = ''.join([params, 'u'])
  269.                 long_params.extend(['gui', 'ui'])
  270.  
  271.             if INTERACTIVE_MODE in self.avail_modes:
  272.                 params = ''.join([params, 'i'])
  273.                 long_params.extend(['interactive', 'text'])
  274.  
  275.             if NON_INTERACTIVE_MODE in self.avail_modes:
  276.                 params = ''.join([params, 'n'])
  277.                 long_params.extend(['noninteractive', 'non-interactive', 'batch'])
  278.  
  279.         if self.supported_ui_toolkits is not None and \
  280.             self.num_supported_ui_toolkits > 1 and prop.gui_build and \
  281.             self.avail_modes is not None and GUI_MODE in self.avail_modes:
  282.  
  283.             if UI_TOOLKIT_QT3 in self.supported_ui_toolkits and UI_TOOLKIT_QT3 in self.installed_ui_toolkits:
  284.                 long_params.extend(['qt3', 'use-qt3'])
  285.  
  286.             if UI_TOOLKIT_QT4 in self.supported_ui_toolkits and UI_TOOLKIT_QT4 in self.installed_ui_toolkits:
  287.                 long_params.extend(['qt4', 'use-qt4'])
  288.  
  289.         if extra_params is not None:
  290.             params = ''.join([params, extra_params])
  291.  
  292.         if extra_long_params is not None:
  293.             long_params.extend(extra_long_params)
  294.  
  295.         opts = None
  296.         show_usage = None
  297.         device_uri = None
  298.         printer_name = None
  299.         error_msg = []
  300.         mode = self.default_mode
  301.         if prop.gui_build:
  302.             ui_toolkit = self.default_ui_toolkit
  303.         else:
  304.             ui_toolkit = 'none'
  305.         lang = None
  306.  
  307.         try:
  308.             opts, self.args = getopt.getopt(sys.argv[1:], params, long_params)
  309.         except getopt.GetoptError, e:
  310.             error_msg = [e.msg]
  311.  
  312.         else:
  313.             for o, a in opts:
  314.                 if o in ('-d', '--device', '--device-uri'):
  315.                     device_uri = a
  316.  
  317.                 elif o in ('-P', '-p', '--printer', '--printer-name'):
  318.                     printer_name = a
  319.  
  320.                 elif o in ('-l', '--logging'):
  321.                     log_level = a.lower().strip()
  322.                     if not log.set_level(log_level):
  323.                         show_usage = 'text'
  324.  
  325.                 elif o in ('-g', '--debug', '--dbg'):
  326.                     log.set_level('debug')
  327.  
  328.                 elif o in ('-u', '--gui', '--ui'):
  329.                     if self.avail_modes is not None and GUI_MODE in self.avail_modes and \
  330.                         self.supported_ui_toolkits is not None and prop.gui_build:
  331.                         mode = GUI_MODE
  332.                     else:
  333.                         error_msg.append("Unable to enter GUI mode.")
  334.  
  335.                 elif o in ('-i', '--interactive', '--text'):
  336.                     if self.avail_modes is not None and INTERACTIVE_MODE in self.avail_modes:
  337.                         mode = INTERACTIVE_MODE
  338.                         ui_toolkit = 'none'
  339.  
  340.                 elif o in ('-n', '--non-interactive', '--batch'):
  341.                     if self.avail_modes is not None and NON_INTERACTIVE_MODE in self.avail_modes:
  342.                         mode = NON_INTERACTIVE_MODE
  343.                         ui_toolkit = 'none'
  344.  
  345.                 elif o in ('-h', '--help'):
  346.                     show_usage = 'text'
  347.  
  348.                 elif o == '--help-rest':
  349.                     show_usage = 'rest'
  350.  
  351.                 elif o == '--help-man':
  352.                     show_usage = 'man'
  353.  
  354.                 elif o == '--help-desc':
  355.                     show_usage = 'desc'
  356.  
  357.                 elif o in ('--qt3', '--use-qt3'):
  358.                     if self.avail_modes is not None and GUI_MODE in self.avail_modes:
  359.                         if self.supported_ui_toolkits is not None and \
  360.                             UI_TOOLKIT_QT3 in self.supported_ui_toolkits and prop.gui_build and \
  361.                             UI_TOOLKIT_QT3 in self.installed_ui_toolkits:
  362.  
  363.                             mode = GUI_MODE
  364.                             ui_toolkit = 'qt3'
  365.                         else:
  366.                             error_msg.append("%s does not support Qt3. Unable to enter GUI mode." % self.mod)
  367.  
  368.                 elif o in ('--qt4', '--use-qt4'):
  369.                     if self.avail_modes is not None and GUI_MODE in self.avail_modes:
  370.                         if self.supported_ui_toolkits is not None and \
  371.                             UI_TOOLKIT_QT4 in self.supported_ui_toolkits and prop.gui_build and \
  372.                             UI_TOOLKIT_QT4 in self.installed_ui_toolkits:
  373.  
  374.                             mode = GUI_MODE
  375.                             ui_toolkit = 'qt4'
  376.                         else:
  377.                             error_msg.append("%s does not support Qt4. Unable to enter GUI mode." % self.mod)
  378.  
  379.                 #elif o in ('-q', '--lang', '--loc'):
  380.                 #    if a.strip() == '?':
  381.                 #        utils.log_title(self.title, self.version)
  382.                 #        self.showLanguages()
  383.                 #        sys.exit(0)
  384.                 #    else:
  385.                 #        lang = utils.validate_language(a.lower())
  386.  
  387.         if error_msg:
  388.             show_usage = 'text'
  389.  
  390.         self.usage(show_usage, error_msg)
  391.  
  392.         if show_usage is not None:
  393.             sys.exit(0)
  394.  
  395.         self.mode = mode
  396.         return opts, device_uri, printer_name, mode, ui_toolkit, lang
  397.  
  398.  
  399.     def showLanguages(self):
  400.         f = tui.Formatter()
  401.         f.header = ("Language Code", "Alternate Name(s)")
  402.         for loc, ll in supported_locales.items():
  403.             f.add((ll[0], ', '.join(ll[1:])))
  404.  
  405.         f.output()
  406.  
  407.  
  408.     def usage(self, show_usage='text', error_msg=None):
  409.         if show_usage is None:
  410.             if not self.quiet:
  411.                 self.showTitle()
  412.             return
  413.  
  414.         if show_usage == 'text':
  415.             self.showTitle()
  416.             log.info()
  417.  
  418.         if show_usage == 'desc':
  419.             print self.doc
  420.  
  421.         else:
  422.             utils.format_text(self.usage_data, show_usage, self.title, self.mod, self.version)
  423.  
  424.             if error_msg:
  425.                 for e in error_msg:
  426.                     log.error(e)
  427.  
  428.                 sys.exit(1)
  429.  
  430.             sys.exit(0)
  431.  
  432.             if show_usage == 'text':
  433.                 sys.exit(0)
  434.  
  435.  
  436.     def showTitle(self, show_ver=True):
  437.         if not self.quiet:
  438.             log.info("")
  439.  
  440.             if show_ver:
  441.                 log.info(log.bold("HP Linux Imaging and Printing System (ver. %s)" % prop.version))
  442.             else:
  443.                 log.info(log.bold("HP Linux Imaging and Printing System"))
  444.  
  445.             log.info(log.bold("%s ver. %s" % (self.title, self.version)))
  446.             log.info("")
  447.             log.info("Copyright (c) 2001-9 Hewlett-Packard Development Company, LP")
  448.             log.info("This software comes with ABSOLUTELY NO WARRANTY.")
  449.             log.info("This is free software, and you are welcome to distribute it")
  450.             log.info("under certain conditions. See COPYING file for more details.")
  451.             log.info("")
  452.  
  453.  
  454.     def getDeviceUri(self, device_uri=None, printer_name=None, back_end_filter=device.DEFAULT_BE_FILTER,
  455.                      filter=device.DEFAULT_FILTER, devices=None, restrict_to_installed_devices=True):
  456.         """ Validate passed in parameters, and, if in text mode, have user select desired device to use.
  457.             Used for tools that are device-centric and accept -d (and maybe also -p).
  458.             Use the filter(s) to restrict what constitute valid devices.
  459.  
  460.             Return the matching device URI based on:
  461.             1. Passed in device_uri if it is valid (filter passes)
  462.             2. Corresponding device_uri from the printer_name if it is valid (filter passes) ('*' means default printer)
  463.             3. User input from menu (based on bus and filter)
  464.  
  465.             device_uri and printer_name can both be specified if they correspond to the same device.
  466.  
  467.             Returns:
  468.                 device_uri|None
  469.                 (returns None if passed in device_uri is invalid or printer_name doesn't correspond to device_uri)
  470.         """
  471.  
  472.         log.debug("getDeviceUri(%s, %s, %s, %s, , %s)" %
  473.             (device_uri, printer_name, back_end_filter, filter, restrict_to_installed_devices))
  474.         log.debug("Mode=%s" % self.mode)
  475.  
  476.         scan_uri_flag = False
  477.         if 'hpaio' in back_end_filter:
  478.             scan_uri_flag = True
  479.  
  480.         device_uri_ok = False
  481.         printer_name_ok = False
  482.         device_uri_ret = None
  483.  
  484.         if devices is None:
  485.             devices = device.getSupportedCUPSDevices(back_end_filter, filter)
  486.             log.debug(devices)
  487.  
  488.         if device_uri is not None:
  489.             if device_uri in devices:
  490.                 device_uri_ok = True
  491.  
  492.             elif restrict_to_installed_devices:
  493.                 log.error("Invalid device URI: %s" % device_uri)
  494.                 device_uri = None
  495.  
  496.             else:
  497.                 device_uri_ok = True
  498.  
  499.         if printer_name is not None:
  500.             printers = device.getSupportedCUPSPrinterNames(back_end_filter, filter)
  501.             if printer_name in printers:
  502.                 printer_name_ok = True
  503.                 printer_name_device_uri = device.getDeviceURIByPrinterName(printer_name, scan_uri_flag)
  504.             else:
  505.                 log.error("Invalid printer name: %s" % printer_name)
  506.                 printer_name = None
  507.  
  508.         if device_uri is not None and printer_name is None and device_uri_ok: # Only device_uri specified
  509.             device_uri_ret = device_uri
  510.  
  511.         elif device_uri is not None and printer_name is not None: # Both specified
  512.             if device_uri_ok and printer_name_ok:
  513.                 if device_uri == printer_name_device_uri:
  514.                     device_uri_ret = device_uri
  515.                 else:
  516.                     log.error("Printer name %s and device URI %s refer to different devices." % (printer_name, device_uri))
  517.                     printer_name, printer_name = None, None
  518.  
  519.         elif device_uri is None and printer_name is not None and printer_name_ok: # Only printer name specified
  520.             device_uri_ret = device.getDeviceURIByPrinterName(printer_name, scan_uri_flag)
  521.  
  522.         elif len(devices) == 1: # Nothing specified, and only 1 device avail.
  523.             device_uri_ret = devices.keys()[0]
  524.             log.info("Using device: %s\n" % device_uri_ret)
  525.  
  526.         if device_uri_ret is None and self.mode in (INTERACTIVE_MODE, NON_INTERACTIVE_MODE) and len(devices):
  527.             device_uri_ret = tui.device_table(devices, scan_uri_flag)
  528.  
  529.         if device_uri_ret is not None:
  530.             user_conf.set('last_used', 'device_uri', device_uri_ret)
  531.  
  532.         else:
  533.             if self.mode in (INTERACTIVE_MODE, NON_INTERACTIVE_MODE):
  534.                 log.error("No device selected/specified or that supports this functionality.")
  535.                 sys.exit(1)
  536.             else:
  537.                 log.debug("No device selected/specified")
  538.  
  539.         return device_uri_ret
  540.  
  541.  
  542.     def getPrinterName(self, printer_name, device_uri, back_end_filter=device.DEFAULT_BE_FILTER,
  543.                        filter=device.DEFAULT_FILTER):
  544.         """ Validate passed in parameters, and, if in text mode, have user select desired printer to use.
  545.             Used for tools that are printer queue-centric and accept -p (and maybe also -d).
  546.             Use the filter(s) to restrict what constitute valid printers.
  547.  
  548.             Return the matching printer_name based on:
  549.             1. Passed in printer_name if it is valid (filter passes) ('*' means default printer)
  550.             2. From single printer_name of corresponding passed in device_uri (filter passes)
  551.             3. User input from menu (CUPS printer list, filtered) [or if > 1 queue for device_uri]
  552.  
  553.             device_uri and printer_name can both be specified if they correspond to the same device.
  554.  
  555.             Returns:
  556.                 (printer_name|None, device_uri|None) (tuple)
  557.                 (returns None if passed in printer_name is invalid or device_uri doesn't correspond to printer_name)
  558.         """
  559.  
  560.         log.debug("getPrinterName(%s, %s, %s, %s)" % (device_uri, printer_name, back_end_filter, filter))
  561.         log.debug("Mode=%s" % self.mode)
  562.  
  563.         device_uri_ok = False
  564.         printer_name_ok = False
  565.         printer_name_ret = None
  566.         device_uri_ret = None
  567.  
  568.         printers = device.getSupportedCUPSPrinterNames(back_end_filter, filter)
  569.         log.debug(printers)
  570.  
  571.         if device_uri is not None:
  572.             devices = device.getSupportedCUPSDevices(back_end_filter, filter)
  573.             if device_uri in devices:
  574.                 device_uri_ok = True
  575.                 device_uri_ret = device_uri
  576.             else:
  577.                 log.error("Invalid device URI: %s" % device_uri)
  578.                 device_uri = None
  579.  
  580.         if printer_name is not None:
  581.             if printer_name == '*':
  582.                 from prnt import cups
  583.                 default_printer = cups.getDefaultPrinter()
  584.                 if default_printer is not None:
  585.                     printer_name_ret = default_printer
  586.                 else:
  587.                     log.error("CUPS default printer not set")
  588.                     printer_name = None
  589.  
  590.             else:
  591.                 if printer_name in printers:
  592.                     printer_name_ok = True
  593.                     device_uri_ret = device.getDeviceURIByPrinterName(printer_name)
  594.                 else:
  595.                     log.error("Invalid printer name")
  596.                     printer_name = None
  597.  
  598.         if device_uri is not None and printer_name is None and device_uri_ok: # Only device_uri specified
  599.             if len(devices[device_uri]) == 1:
  600.                 printer_name_ret = devices[device_uri][0]
  601.  
  602.         elif device_uri is not None and printer_name is not None: # Both specified
  603.             if device_uri_ok and printer_name_ok:
  604.                 if device_uri == device_uri_ret:
  605.                     printer_name_ret = printer_name
  606.                 else:
  607.                     log.error("Printer name and device URI refer to different devices.")
  608.  
  609.         elif device_uri is None and printer_name is not None and printer_name_ok: # Only printer name specified
  610.             printer_name_ret = printer_name
  611.  
  612.         elif len(printers) == 1: # nothing specified, and only 1 avail. printer
  613.             printer_name_ret = printers[0]
  614.             log.info("Using printer: %s\n" % printer_name_ret)
  615.  
  616.         if printer_name_ret is None and self.mode in (INTERACTIVE_MODE, NON_INTERACTIVE_MODE) and len(printers):
  617.             printer_name_ret = tui.printer_table(printers)
  618.  
  619.         if printer_name_ret is not None and device_uri_ret is None:
  620.             device_uri_ret = device.getDeviceURIByPrinterName(printer_name_ret)
  621.  
  622.         if device_uri_ret is not None:
  623.             user_conf.set('last_used', 'device_uri', device_uri_ret)
  624.  
  625.         if printer_name_ret is not None:
  626.             user_conf.set('last_used', 'printer_name', printer_name_ret)
  627.  
  628.         else:
  629.             if self.mode in (INTERACTIVE_MODE, NON_INTERACTIVE_MODE):
  630.                 log.error("No printer selected/specified or that supports this functionality.")
  631.                 sys.exit(1)
  632.             else:
  633.                 log.debug("No printer selected/specified")
  634.  
  635.         return printer_name_ret, device_uri_ret
  636.  
  637.  
  638.     def lockInstance(self, suffix=''):
  639.         if suffix:
  640.             ok, self.lock_file = utils.lock_app('-'.join([self.mod, suffix]))
  641.         else:
  642.             ok, self.lock_file = utils.lock_app(self.mod)
  643.  
  644.         if not ok:
  645.             sys.exit(1)
  646.  
  647.  
  648.     def unlockInstance(self):
  649.         if self.lock_file is not None:
  650.             utils.unlock(self.lock_file)
  651.